<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">
      <el-form-item label="项目名字:" prop="itemName">
        <el-input v-model="queryData.itemName" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('itemName')"/>
      </el-form-item>
      <el-form-item label="项目类型:" prop="itemType">
        <el-input v-model="queryData.itemType" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('itemType')"/>
      </el-form-item>

      <el-form-item label="上架状态:" prop="shelfStatus">
        <el-select v-model="queryData.shelfStatus" placeholder="请选择" clearable @clear="formClear('shelfStatus')"
                   style="width: 200px">
          <el-option label="未上架" value="0"/>
          <el-option label="上架" value="1"/>
        </el-select>
      </el-form-item>

      <el-form-item label="分类:" prop="type">
        <el-select v-model="queryData.type" placeholder="请选择" style="width: 100%" clearable @clear="formClear('type')">
          <el-option :label="item.name" :value="item.code - 0" v-for="(item, index) in typeList" :key="index"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="lookOverEvent(null)">新 增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column type="index" label="序号" align="center" width="80"/>
      <el-table-column prop="itemCover" label="项目封面" align="center" width="150">
        <template slot-scope="{row}">
          <el-image :src="row.itemCover" style="width: 80px;height: 80px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="itemName" label="项目名字" align="center"/>
      <el-table-column prop="itemTag" label="项目标签" align="center"/>
      <el-table-column prop="itemType" label="项目类型" align="center" width="130" show-overflow-tooltip/>
      <el-table-column prop="shopCount" label="门店数量" align="center" width="130" show-overflow-tooltip/>
      <el-table-column prop="itemJoinNanoMin" label="项目加盟费区间最低" align="center" width="180"/>
      <el-table-column prop="itemJoinNanoMax" label="项目加盟费区间最高" align="center" width="180" show-overflow-tooltip/>
      <el-table-column label="上架状态" align="center" width="100">
        <template slot-scope="{row}">
          <el-switch :value="row.shelfStatus" active-color="#13ce66"
                     :active-value="1" :inactive-value="0" @change="switchChange($event, row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="lookOverEvent(row)">查 看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
  </div>
</template>

<script>
import {mapGetters} from "vuex";

export default {
  computed: {
    ...mapGetters(['confirm'])
  },
  name: 'enterpriseServe',
  data() {
    return {
      tableList: [],
      typeList: [],
      queryData: {
        itemName: null, // 项目名字
        itemType: null, // 项目类型
        shelfStatus: null, // 上架状态：0未上架；1上架
        type: null, // 类型资源表code
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      entity: null,
      drawer: false
    }
  },
  beforeMount() {
    this.getRequest()
    this.getTypeList()
  },
  methods: {
    /**
     * 上下架事件
     */
    switchChange(event, row) {
      this.$confirm('确认要修改当前上下架状态吗?', '提示', this.confirm.options).then(async() => {
        await this.$ajax.post(`/wx/enterprise/serve/update`, { id: row.id, shelfStatus: event })
        this.$message.success('操作成功!')
        this.getRequest()
      })
    },
    /**
     * 新增 or 查看按钮事件
     */
    lookOverEvent(row) {
      const detailId = row ? row.id : ''
      this.$router.push(`/enterpriseManage/enterpriseServeAdd?detailId=${detailId}`)
    },
    /**
     * 重置事件
     */
    onReset() {
      this.queryData.pageNum = 1
      this.queryData.province = null
      this.queryData.city = null
      this.queryData.county = null
      this.$refs['queryData'].resetFields()
      this.getRequest()
    },
    /**
     * 查询条件提交事件
     */
    onSubmit() {
      this.queryData.pageNum = 1
      this.getRequest()
    },
    /**
     * 列表请求事件
     */
    async getRequest() {
      const params = JSON.parse(JSON.stringify(this.queryData))
      const res = await this.$ajax.get('/wx/enterprise/serve/backstage/list', {params})
      this.queryData.total = res.total
      this.tableList = res.rows
    },
    /**
     * form-input 置空事件
     */
    formClear(key) {
      this.queryData[key] = null
      this.onSubmit()
    },
    /**
     * 获取类型
     */
    async getTypeList() {
      const res = await this.$ajax.get('/resource/table/list?type=qiyefuwu')
      this.typeList = res.rows
    },
  }
}
</script>

<style scoped>

</style>
